import '../utils/crypto1/crypto/crypto'
import '../utils/crypto1/hmac/hmac'
import '../utils/crypto1/sha1/sha1'
import { Base64 } from 'js-base64'

/**
 * 生成policy和签名
 * 默认1小时后失效
 * @param {*} accessKeySecret oss accessKeySecret
 * @param {*} expiration 过期时间 单位秒
 * @returns
 */
export const genSignature = (accessKeySecret, expir = 1200) => {

  const expiration = (new Date(Date.now() +  expir * 1000)).toISOString()
  const policy = {
    expiration: expiration, // 失效时间
    conditions: [
      ['content-length-range', 0, 469824028] // 设置上传文件的大小限制 1G
    ]
  }
  const policyBase64 = Base64.encode(JSON.stringify(policy))

  const bytes = Crypto.HMAC(Crypto.SHA1, policyBase64, accessKeySecret, { asBytes: true });
  const signature = Crypto.util.bytesToBase64(bytes);

  return {
    policy:policyBase64,
    signature
  }
}
